﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using eStore.Models;
using eStore.ViewModels;

namespace eStore.Controllers
{
    public class AccountController : Controller
    {
        private IUserRepository _userRepo;
        public AccountController():this(new UserRepository()) { 
        
        }

        public AccountController(IUserRepository userRepo)
        {
            _userRepo = userRepo;
        }

        //
        // GET: /Account
        [Authorize()]
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Logon(String ReturnUrl)
        {
            AccountLogonViewModel oViewModel = new AccountLogonViewModel() { ReturnUrl = ReturnUrl, PageTitle = "User Logon Form", MyLogOnModel = new LogOnModel() };
            return View(oViewModel);
        }

        [HttpPost]
        public ActionResult Logon(LogOnModel MyLogOnModel, String ReturnUrl)
        {
            if(ModelState.IsValid)
            {
                var user= _userRepo.GetUserByName(MyLogOnModel.UserName);
                if (user != null)
                {
                    if (user.Password != MyLogOnModel.Password)
                        ModelState.AddModelError("_Form", "Invalid credentials! Please relogin!");
                    else
                    {
                        FormsAuthentication.SetAuthCookie(user.UserName, MyLogOnModel.RememberMe == true);
                        MigrateCart(user.UserName);
                        if (!(String.IsNullOrEmpty(ReturnUrl)))
                            return Redirect(ReturnUrl);
                        else
                            return Redirect(FormsAuthentication.DefaultUrl); 
                    }
                }
                else {
                    FormsAuthentication.SignOut();
                    ModelState.AddModelError("_Form", "Invalid credentials or no such user!");
                }
            }
            MyLogOnModel.Password = "";
            AccountLogonViewModel oViewModel = new AccountLogonViewModel() { ReturnUrl = ReturnUrl, PageTitle = "User Logon Form", @MyLogOnModel = MyLogOnModel};
          
            return View(oViewModel);
        }

        public ActionResult LogOut() {
            FormsAuthentication.SignOut();
            var shoppingCart = ShoppingCart.GetCart(this.HttpContext, false);
                
            return RedirectToAction("Index", "Home", null);
        }

        [NonAction]
        private bool MigrateCart(string sUserName)
        {
            bool blnMigrated = false;
            try
            {
                var shoppingCart = ShoppingCart.GetCart(this.HttpContext, false);
                var cart = shoppingCart.MigrateCart(sUserName);
                blnMigrated = cart != null;
                Session[ShoppingCart.CartSessionKey] = sUserName;
            }
            catch (Exception)
            {
                blnMigrated = false;
            }
            return blnMigrated;
            
        }

    }
}
